package luo.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import luo.Exception.LoginException;
import luo.Mapper.UserMapper;
import luo.Service.UserService;
import luo.entity.User;
import luo.utils.PasswordUtil;
import luo.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Author:let_free
 * @Date:2024/10/3/14:34
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public String login(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        String encodedPassword = PasswordUtil.hashPassword(password);
        queryWrapper.eq("user_name", username);
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            throw new LoginException("用户名错误");
        } else {
//            验证通过  根据用户名生成token
            Boolean result = PasswordUtil.verifyPassword(password, user.getPassword());
            if (!result) {
                throw new LoginException("密码错误");
            }
            return TokenUtils.generateToken(username);
        }
    }
}
